/*
 * @Descripttion: 微前端配置入口
 * @version: 1.0.0
 * @Author: shuaishuai.han
 * @Date: 2022-01-05 22:13:32
 */
import Vue from 'vue';
import { registerMicroApps, start, initGlobalState } from 'qiankun';
import router from '../src/router';
import store from '../src/store';
import MicroApps from './app-config';
// import * as types from '@/store/mutations-types';

const _initState = {
  userInfo: store.state.userInfo // 用户信息
};

const apps = MicroApps.filter((item) => item.isOpen).map((item) => {
  const { name, entry, container, route } = item;
  return {
    name,
    entry,
    container,
    activeRule: route,
    props: {
      MicroRouter: (info) => {
        // if (info === '/login') store.commit(types.SET_USERINFO_MUTATION, {});
        router.push(info);
      }
    }
  };
});

const actions = initGlobalState(_initState);
actions.onGlobalStateChange((state, prev) => {
  // state: 变更后的状态; prev 变更前的状态
  // console.log('state1===', state);
  // console.log('prev1===', prev);
}, true);
// 设置全局方法
Vue.prototype.$setGlobalState = actions.setGlobalState;

registerMicroApps(apps, {
  beforeLoad: () => {
    // 加载子应用 开启loading
    document.getElementById('mainLoading').style.display = 'flex';
  },
  afterMount: () => {
    // 子应用加载完毕 关闭loading
    document.getElementById('mainLoading').style.display = 'none';
  }
});
start({ prefetch: false, sandbox: { strictStyleIsolation: true } });
